//
//  AlbumOfListCell.m
//  SuperRadio
//
//  Created by GYRu on 14-12-22.
//  Copyright (c) 2014年 三个臭皮匠. All rights reserved.
//

#import "AlbumOfListCell.h"
#import "AlbumOfList.h"
#import "UIImageView+WebCache.h"

@interface AlbumOfListCell ()

@property(nonatomic, retain) UIImageView *albumImageView;
@property(nonatomic, retain) UIImageView *smallImageView;
@property(nonatomic, retain) UILabel *title;
@property(nonatomic, retain) UILabel *programCounts;
@property(nonatomic, retain) UILabel *counts;
@property(nonatomic, retain) UILabel *updateTime;
@property(nonatomic, retain) UILabel *time;

@end



@implementation AlbumOfListCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.albumImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.albumImageView];
        [_albumImageView release];
        
        self.smallImageView = [[UIImageView alloc] init];
//        self.smallImageView.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:self.smallImageView];
        [_smallImageView release];
        
        self.title = [[UILabel alloc] init];
        [self.contentView addSubview:self.title];
        self.title.numberOfLines = 2;
        [_title release];
        
        
        self.counts = [[UILabel alloc] init];
        self.counts.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.counts];
        [_counts release];
        
        
        self.time = [[UILabel alloc] init];
        self.time.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.time];
        [_time release];
        
        
        
    }
    return self;
}



-(void)setAlbumList:(AlbumOfList *)albumList
{
    if (_albumList != albumList) {
        [_albumList release];
        _albumList = [albumList retain];
    }
    
    _title.text = albumList.title;
    [_albumImageView setImageWithURL:[NSURL URLWithString:[albumList.coverSmall stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    _counts.text = [NSString stringWithFormat:@"节目数 %@", [albumList.tracks description]];
    
    // 时间戳的转换
    // 从1970年1月1日到现在的时间再除以1000 (因为album.lastUptrackAt是微秒), 得到秒数
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:([albumList.lastUptrackAt integerValue] / 1000)];
    // 在NSDate 和 NSString 之间相互转换的格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 设置一个日期格式,让字符串能按照设定的格式转为相应的日期
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    // 将时间转换为字符串
    NSString *strDate = [formatter stringFromDate:date];
    
    _time.text = [NSString stringWithFormat:@"最近更新 %@", [strDate substringToIndex:10]];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ttpodicon" ofType:@"png"];
    self.smallImageView.image = [[UIImage imageWithContentsOfFile:path] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width = self.contentView.frame.size.width / 6;
    CGFloat height = self.contentView.frame.size.height / 3;
    CGFloat x = self.contentView.frame.origin.x;
    CGFloat y = self.contentView.frame.origin.y;
    
    self.albumImageView.frame = CGRectMake(x + 8, y + 5, width, height * 3 - 10);
    self.smallImageView.frame = CGRectMake(width + 13, y + 5, width / 2, width / 2);
    self.title.frame = CGRectMake(width + width / 2 + 18, y + 5, width * 4 , height * 2);
    self.counts.frame = CGRectMake(width + 11, height * 2 +5, width * 2, height - 10);
    self.time.frame = CGRectMake(width * 3, height * 2 +5, width * 3, height - 10);
    [self.title sizeToFit];
}

- (void)dealloc
{
    [_albumList release];
    
    [_albumImageView release];
    [_smallImageView release];
    [_counts release];
    [_time release];
    [_title release];
    
    [super dealloc];
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
